#!/usr/bin/env perl -w

# CS4414, Lab 7
# For use with the "extract" program.
#
use strict;
use File::Basename;
use File::Copy;
use feature "switch";
use Getopt::Long;
use Pod::Usage;

my $quiet = undef;
my $mode = "copy";
my $format = "[0-9]{5}[.]file";
my $dir = ".";
my $help = 0;

GetOptions ('quiet' => \$quiet,
            'dir=s' => \$dir,
            'format=s' => \$format,
            'mode=s' => \$mode,
            'help' => \$help);
            
pod2usage(-message => "Invalid mode, use -help to get usage",  -verbose => 1) if !($mode =~ /copy|move/);
pod2usage(-verbose => 1) if $help;

sub myprint 
{
    my $str = shift;
    if (!$quiet) 
    {
        print "$str\n";
    }
}

sub do_rename
{
    my @all = grep(/$format/, `find .`);

    scalar(@all) != 0 or die("No files found in \"$dir\" to rename: terminating.");

    foreach my $file(@all) 
    { 
        chomp $file;
        
        my $filetype = `file $file`;
        chomp $filetype;
        my $ext = undef;
      
        given ($filetype) 
        {
            when(/.*TIFF image.*/i)          { $ext = "tiff"; }
            when(/.*GIF image.*/i)           { $ext = "gif"; }
            when(/.*PNG image.*/i)           { $ext = "png"; }
            when(/.*JP.?G image.*/i)         { $ext = "jpg"; }
            when(/.*PDF document.*/i)        { $ext = "pdf"; }
            when(/.*PostScript document.*/i) { $ext = "ps"; }
            when(/.*HTML document.*/i)       { $ext = "html"; }
            when(/.*executable.*/i)          { $ext = "exe"; }
            when(/.*ASCII.*/i)               { $ext = "txt"; }
        }
        
        my $base = basename($file, "file");
        my $dest_dir = dirname($file);
        if (!$ext)
        {
            myprint("file $base has unknown extension");
            next;
        }

        myprint("Moving $file to $base$ext");
        my $dest = "$dest_dir/$base$ext";
        my $success = ($mode =~ "copy") ? copy($file, $dest)
                                        : rename($file, $dest);

        $success or myprint("Failed to move file $file");

    }
}

do_rename();

__END__
=head1 NAME

identify

=head1 SYNOPSIS

perl identify.pl [options]

B<identify> will attempt to rename files with the given pattern to their correct file extensions.

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-dir=DIRECTORY>

Sets the directory to search for the files to be renamed. The default value is the current working directory in which the script is invoked.

=item B<-format=FORMAT>

Specifies the format of files to be renamed as a perl regular expression (do not include beginning and ending /'s). By default "[0-9]{5}[.]file"

=item B<-quiet>

If set, the program produces no output to stdout. Off by default.

=item B<-mode=MODE>

Sets the action to be performed on each file. Must be one of "copy" or "move".

=back

=cut
